package com.globallogic.vacationplanner.model.service.impl;

import java.io.Serializable;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.globallogic.vacationplanner.model.dao.declaration.IUserDao;
import com.globallogic.vacationplanner.model.dao.declaration.IVacationDao;
import com.globallogic.vacationplanner.model.entity.User;
import com.globallogic.vacationplanner.model.entity.Vacation;
import com.globallogic.vacationplanner.model.helper.DtoHelper;
import com.globallogic.vacationplanner.transfer.dto.impl.VacationDto;
import com.globallogic.vacationplanner.transfer.exception.DaoException;
import com.globallogic.vacationplanner.transfer.service.IVacationService;

@Service("vacationService")
public class VacationService implements IVacationService, Serializable {

	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(VacationService.class);
	private IVacationDao vacationDao;
	private IUserDao userDao;

	public VacationService() {
		LOGGER.info("User service was created successfully");
	}

	@Autowired
	public void setVacationDao(IVacationDao vacationDao) {
		this.vacationDao = vacationDao;
	}

	@Autowired
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	@Transactional
	public void addNewVacation(VacationDto vacationDto) throws DaoException {
		LOGGER.info("start to add new vacation");
		Format formatter = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = formatter.format(vacationDto.getStartDate());
		String endDate = formatter.format(vacationDto.getEndDate());
		User userr = new User();
		userr.setLogin(vacationDto.getLogin());
		User user = userDao.read(userr);
		Vacation vacation = new Vacation(user, vacationDto.getDescription(),
				startDate, endDate);
		LOGGER.info("configuring of new vacation was finished succes");
		vacationDao.insert(vacation);
		LOGGER.info("Add new vacation success");
	}

	@Override
	@Transactional
	public List<VacationDto> fillTable(VacationDto vacationDto) {
		LOGGER.info("fill vacation tale method");
		User template = new User();
		template.setLogin(vacationDto.getLogin());
		User creator = userDao.read(template);

		Set<Vacation> vacationList = creator.getVacations();
		List<Vacation> buffer = new ArrayList<>(vacationList);

		List<VacationDto> vacationDtoList = DtoHelper
				.convertVacationListToDto(buffer);
		return vacationDtoList;
	}

	@Override
	@Transactional
	public void deleteVacationRow(VacationDto deletedObject) {
		LOGGER.info("Start to delete vacation from table");
		Vacation deletedVacation = new Vacation();
		Format formatter = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = formatter.format(deletedObject.getStartDate());
		String endDate = formatter.format(deletedObject.getEndDate());

		deletedVacation.setStartDate(startDate);
		deletedVacation.setEndDate(endDate);
		deletedVacation.setDescription(deletedObject.getDescription());
		LOGGER.info("configuring of new vacation was finished succes");

		vacationDao.deleteVacationRow(deletedVacation);
		LOGGER.info("Delete vacation success");
	}

}
